package com.chukun.multi.halfsync_halfasync;

import com.chukun.multi.enums.AlarmType;
import com.chukun.multi.twophase.AlarmMgr;
import org.apache.log4j.Logger;

import java.sql.Connection;

public class SampleAlarmClient {

    private static Logger logger = Logger.getLogger(SampleAlarmClient.class);

    private static final int ALARM_MSG_SUPRESS_THRESHOLD = 10;

    static {
        AlarmMgr.getInstance().init();
    }

    public static void main(String[] args) {
        SampleAlarmClient sampleAlarmClient = new SampleAlarmClient();
        Connection dbConnection = null;
        try{
            dbConnection = sampleAlarmClient.retrieveDBConnection();
        }catch (Exception e) {
            final AlarmMgr alarmMgr = AlarmMgr.getInstance();
            // 报警被重复发送至报警模块的次数
            int duplicateSubmissionCount;
            String alarmId = "0000000020";
            final String alarmExtraInfo = "operation=GetDBConnection;detail=fail to get connection "+e.getMessage();
            duplicateSubmissionCount = alarmMgr. sendAlarm(AlarmType.FAULT,alarmId,alarmExtraInfo);
            if (duplicateSubmissionCount<ALARM_MSG_SUPRESS_THRESHOLD) {
                logger.error("alarm["+alarmId+"] raised, extraInfo: "+alarmExtraInfo);
            }else{
                if (duplicateSubmissionCount==ALARM_MSG_SUPRESS_THRESHOLD){
                    logger.error("alarm["+alarmId+"] raised, more than : "+ALARM_MSG_SUPRESS_THRESHOLD+" times");
                }
            }
            return;
        }
    }

    private Connection retrieveDBConnection() {
        // 省略代码
        return null;
    }
}
